package com.addzero.web.ui.system.hooks

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.addzero.web.ui.system.dynamicroute.RouteMetadata
import com.addzero.web.ui.system.hooks.side_menu.UseSideMenu

@Composable
fun renderNaviItem(
    useSideMenu: UseSideMenu,
    meta: RouteMetadata,
    isSelected: Boolean,
    hasChildren: Boolean,
    isExpanded: Boolean = true, // 默认展开
) {
    // 获取当前节点是否展开的状态
    val isNodeExpanded = useSideMenu.expandedNodes.contains(meta.routerPath)

    // 修复：使用效果记住展开状态
    var internalExpanded by remember { mutableStateOf(isNodeExpanded) }

    // 当展开状态变化时，更新内部状态
    LaunchedEffect(isNodeExpanded) {
        internalExpanded = isNodeExpanded
    }

    NavigationDrawerItem(
        label = { 
            if (isExpanded) {
                // 当侧边栏展开时显示文本，处理文本溢出
                Text(
                    text = meta.title,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
            } else {
                // 当侧边栏收起时，仅显示图标，不显示文本
            }
        },
        selected = isSelected,
        onClick = {
            // 如果有子节点，切换展开状态；否则，切换路由
            if (hasChildren) {
                useSideMenu.toggleNodeExpansion(meta.routerPath)

                // 立即更新内部状态
                internalExpanded = !internalExpanded

                // 重要：强制子节点渲染
                // 找到UseListTree实例，确保更新和重绘
                useSideMenu.forceRefresh()
            } else {
                useSideMenu.currentRoute = meta.routerPath
            }
        },
        modifier = Modifier.padding(vertical = 4.dp),
        icon = {
            // 使用meta.icon如果有，否则根据节点类型使用默认图标
            meta.icon?.let { icon ->
                Icon(
                    imageVector = icon,
                    contentDescription = meta.title,
                    modifier = Modifier.size(24.dp)
                )
            }
        },
        badge = {
            // 仅当侧边栏展开且有子节点时，显示展开/折叠图标
            if (isExpanded && hasChildren) {
                val arrowIcon = if (internalExpanded) {
                    Icons.Default.KeyboardArrowDown
                } else {
                    Icons.AutoMirrored.Filled.KeyboardArrowRight
                }
                Icon(
                    imageVector = arrowIcon,
                    contentDescription = if (internalExpanded) "折叠" else "展开",
                    modifier = Modifier.size(24.dp)
                )
            }
        },
        shape = RoundedCornerShape(8.dp),
        colors = NavigationDrawerItemDefaults.colors(
            selectedContainerColor = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f),
            unselectedContainerColor = MaterialTheme.colorScheme.surface
        )
    )
}
